var sys;//top.sys.userName(1);
var stompClient = null;
$(function () {
    sys = sysInfo();
    //默认打开控制台页面
    open('templates/animal/control/index.html');
    open('templates/animal/banner/banner.html');
    connect();
    // pushWarnInfoToWeb();

    //获取登录信息 start
    function sysInfo() {
        var users;
        var depts;
        var dicts;
        $.ajax({
            cache: true,
            type: "GET",
            url: top.ctx + "/sysInfo",
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                users = data.users;
                depts = data.depts;
                dicts = data.dicts;
            }
        });
        return {
            userName: function (id) {
                try {
                    return users[id].name;
                } catch (e) {
                    return '';
                }
            }, userAttr: function (id, field) {
                try {
                    return users[id].name;
                } catch (e) {
                    return '';
                }
                return users[id][field];
            }, user: function (id) {
                try {
                    return users[id];
                } catch (e) {
                    return '';
                }

            },
            deptName: function (id) {
                try {
                    return depts[id].name;
                } catch (e) {
                    return '';
                }

            }, deptAttr: function (id, field) {
                try {
                    return depts[id][field];
                } catch (e) {
                    return '';
                }

            }, dept: function (id) {
                try {
                    return depts[id];
                } catch (e) {
                    return '';
                }

            }, dictName: function (type, value) {
                try {
                    return dicts[type][value].name;
                } catch (e) {
                    return '';
                }

            }
        }
    }//获取登录信息 end

    //websoket 连接 start
    function connect() {
        var sock = new SockJS(ctx + "/endpointChat");
        var stomp = Stomp.over(sock);
        stomp.connect('guest', 'guest', function (frame) {

            /**  订阅了/user/queue/notifications 发送的消息,这里雨在控制器的 convertAndSendToUser 定义的地址保持一致, 
             *  这里多用了一个/user,并且这个user 是必须的,使用user 才会发送消息到指定的用户。 
             *  */
//	         stomp.subscribe("/user/queue/notifications", handleNotification);
            stomp.subscribe('/topic/getResponse', function (response) { //订阅/topic/getResponse 目标发送的消息。这个是在控制器的@SendTo中定义的。
                toastr.options = {
                    "closeButton": true,
                    "debug": false,
                    "progressBar": true,
                    "positionClass": "toast-bottom-right",
                    "onclick": null,
                    "showDuration": "400",
                    "hideDuration": "1000",
                    "timeOut": "7000",
                    "extendedTimeOut": "1000",
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut"
                }
                toastr.info(JSON.parse(response.body).responseMessage);
            });

            stomp.subscribe('/topic/warn', function (response) {
                subscribeOpenWin(response);
            });
            pushWarnInfoToWeb();
        });

    }//websoket 连接 end
});
//触发告警
function pushWarnInfoToWeb() {
    $.ajax({
        cache: true,
        type: "GET",
        url:  "/animal/warningRecord/pushWarnInfoToWeb",
        async: false
    });
}
//处理告警
function handleWarn() {
    $.ajax({
        cache: true,
        type: "GET",
        url:  "/animal/warningRecord/handleWarn",
        async: false
    });
}

function subscribeOpenWin(response){
    console.log(JSON.parse(response.body));
    var ret = JSON.parse(response.body);
    var msg = '当前基站/耳标数据异常，总共产生'+ret.allSum+'个告警信息，其中'+ret.baseStation+'个基站失控告警，'+ret.tag+'个耳标失控告警，'+ret.sick+'个动物疾病预警，'+ret.parturition+'个动物生产预警，'+ret.oestrus+'个动物发情预警！';
    layer.open({
        type: 1,
        content: '<div style="padding: 20px 40px;">' + msg + '<a onclick="parent.open(\'animal/warningRecord\');handleWarn();layer.closeAll();">点击前往告警中心</a>' + '</div>',
        btn: '知道了' ,
        btnAlign: 'c',
        // shade: 1 ,
        yes: function () {
            handleWarn();
            layer.closeAll();
        }
    });
}


